﻿var x0;
var y0;
var objlayer=new Array();
objlayer[0]="Layer1";
var dx=new Array();
var dy=new Array();
var dragenable2=new Array();
var xx0;
var yy0;
var dragenable=0;
var selectenable=0;
var coord=new Array();
function initselect() {
    var obj=event.srcElement;
    if (obj!=selectarea) {           
        if(event.clientY<= coord[3]){
            x0=document.body.scrollLeft+event.clientX;
            y0=document.body.scrollTop+event.clientY; 
            selectenable=1;
        }
    }
}
function destroyselect(){
    selectenable=0;
    selectarea.style.visibility='hidden';
    selectarea.style.width = 0;
    selectarea.style.height = 0;
}
function startselect() {
    if (selectenable==1) {       
        selectarea.style.visibility='visible';
        if(document.body.scrollLeft+event.clientX-x0>0) {
	        selectarea.style.left=document.documentElement.scrollLeft+x0;
		    selectarea.style.width=document.body.scrollLeft+event.clientX-x0;
		}
        else{
		    selectarea.style.left=document.documentElement.scrollLeft+event.clientX;
		    selectarea.style.width=x0-(document.body.scrollLeft+event.clientX);
		}
        if (document.body.scrollTop+event.clientY-y0>0) {
		    selectarea.style.top=document.documentElement.scrollTop+y0;	
		    selectarea.style.height=document.body.scrollTop+event.clientY-y0;
		}
        else{
		    selectarea.style.top=document.documentElement.scrollTop+event.clientY;
		    selectarea.style.height=y0-(document.body.scrollTop+event.clientY);
		}	
	}
}
function endselect() {
    selectenable=0;
    for (i=0; i<objlayer.length; i++) {
        layername=eval(objlayer[i]);
	    var dx2=(selectarea.offsetLeft+selectarea.offsetWidth)-(layername.offsetLeft+layername.offsetWidth);
	    var dy2=(selectarea.offsetTop+selectarea.offsetHeight)-(layername.offsetTop+layername.offsetHeight);	   
        if (selectarea.offsetLeft-1<layername.offsetLeft && dx2>-1 && selectarea.offsetTop-1<layername.offsetTop && dy2>-1) {
	        dx[i]=layername.offsetLeft-selectarea.offsetLeft;
	        dy[i]=layername.offsetTop-selectarea.offsetTop;
	        dragenable2[i]=1;
	        }
        else {dx[i]=0;dy[i]=0;dragenable2[i]=0;}
    }
    window.status=dragenable2+':'+dx+':'+dy;		
    //end
}
function initdrag() {   
    xx0=document.documentElement.scrollLeft+event.clientX-selectarea.offsetLeft;
    yy0=document.documentElement.scrollTop+event.clientY-selectarea.offsetTop;
    dragenable=1;
}
function startdrag() {
    if (dragenable==1) {
	    selectarea.style.left=document.documentElement.scrollLeft+event.clientX-xx0;
	    selectarea.style.top=document.documentElement.scrollTop+event.clientY-yy0;
	    for (i=0; i<objlayer.length; i++) {
	        layername=eval(objlayer[i]);
		    if (dragenable2[i]==1) {
		        layername.style.left=selectarea.offsetLeft+dx[i];
		        layername.style.top=selectarea.offsetTop+dy[i];		        
		    }
		}
	    window.status=dx+':'+dy+':'+dragenable2;
	}
}
function checkmargin(){
    if(selectarea.style.visibility=="visible"){             
        if(event.clientX<=coord[0]){           
            selectarea.style.left=coord[0];
            selectarea.style.width=x0-coord[0];        
        }
        if(event.clientY<=coord[1]){
            selectarea.style.top=coord[1];
            selectarea.style.height=y0-coord[1];
        }             
        if(event.clientX>=coord[2]){
            selectarea.style.width=coord[2]-selectarea.style.posLeft;
        }
        if(event.clientY>=coord[3]){
            selectarea.style.height=coord[3]-selectarea.style.posTop;
        }        
    }
}
function enddrag() {
    dragenable=0;
    for (i=0; i<objlayer.length; i++) {dragenable2[i]=0;}
}
function ContainerPosition(){
    var layer = document.getElementById("Layer1");
    var ary = new Array();
    ary[0]=layer.offsetLeft;
    ary[1]=layer.offsetTop;
    ary[2]=layer.offsetWidth;
    ary[3]=layer.offsetHeight; 
    while(layer = layer.offsetParent){
        ary[0] += layer.offsetLeft;
        ary[1] += layer.offsetTop;        
    }
     ary[2]+=ary[0];
     ary[3]+=ary[1];
    
    return ary;  
}
function selectChange(){
    var x = selectarea.offsetLeft;
    var y = selectarea.offsetTop;
    var x1 = selectarea.offsetWidth+x;   
    var y1 = selectarea.offsetHeight+y;       
    if(x<=coord[0])x=coord[0];
    if(y<=coord[1])y=coord[1];
    if(x1>=coord[2])x1=coord[2];   
    if(y1>=coord[3])y1=coord[3];
    var resultx0=parseInt((x-coord[0])/17);
    var resulty0=parseInt((y-coord[1])/17); 
    var resultx1=0;
    if(x1==coord[2]){resultx1=24;}
    else{resultx1=parseInt(24-(coord[2]-x1)/17)+1;}                
    var resulty1=0;
    if(y1==coord[3]){resulty1=7;}
    else{resulty1=parseInt(7-(coord[3]-y1)/17)+1;} 
    var o = document.getElementById("Layer1"); 
    var firstele=24*resulty0+resultx0+1;
    var color="#a9a9a9";
    if(o.childNodes[firstele].style.backgroundColor=="#a9a9a9")
        color="#2b7dbb";
    else if(o.childNodes[firstele].backgroundColor=="#2b7dbb")
        color="#a9a9a9";
    for(var i=resulty0;i<resulty1;i++){
        for(var j=resultx0;j<resultx1;j++){
            var n=24*i+j+1;
            o.childNodes[n].style.backgroundColor=color;
        } 
    }
}
function loadData(obj){
    var index=Math.floor(obj.value)-1;
    var layer=document.getElementById("Layer1"); 
    var form=document.form1;
    form.filenameSuffix.value=plans[index][0];
    form.splitTime.value=plans[index][1];          
    if(plans[index][2]==1)
        form.recordEnable.checked="checked"; 
    else
        form.recordEnable.checked="";
    if(plans[index][3]==1)
        form.recycleRecEnable.checked="checked";
    else
        form.recycleRecEnable.checked="";        
    var str=plans[index][4];
    for(var i=1;i<=168;i++){
        if(str.substring(i-1,i)=="1"){
            layer.childNodes[i].style.backgroundColor="#2b7dbb";
        }else if(str.substring(i-1,i)=="0"){
            layer.childNodes[i].style.backgroundColor="#a9a9a9";
        }
    }
}
function loadStroage(){       
    coord= ContainerPosition(); 
    var chn=document.getElementById("chn");
    for(var i=1;i<=chnNum;i++){        
        var item = new Option("CHN "+i, i);      
        chn.options.add(item);  
    }   
    var _tr=document.getElementById("nums").rows[0]; 
    for(var i=0;i<24;i++){
         var _td=_tr.insertCell();
         _td.className="num";
         _td.innerHTML="<span>"+i.toString()+"</span>"
    }
    var form=document.form1;
    form.filenameSuffix.value=plans[0][0];
    form.splitTime.value=plans[0][1];
    if(plans[0][2]==1)form.recordEnable.checked="checked"; 
    if(plans[0][3]==1)form.recycleRecEnable.checked="checked";
    form.recycleMode.value=plans[0][5];
    str=plans[0][4];
    var layer=document.getElementById("Layer1");
    for(var i=0;i<168;i++){
        var btn=document.createElement("<input type='button'/>");                
        btn.style.height=17;
        btn.style.width=17;
        btn.id=i.toString();
        layer.appendChild(btn);        
        if(str.substring(i,i+1)=="1"){
            layer.childNodes[i+1].style.backgroundColor="#2b7dbb";
        }else if(str.substring(i,i+1)=="0"){
            layer.childNodes[i+1].style.backgroundColor="#a9a9a9";
        }
        btn.onclick=function(){        
            if(this.style.backgroundColor=="#a9a9a9"){   
                this.style.backgroundColor="#2b7dbb";
            }else if(this.style.backgroundColor=="#2b7dbb"){
                this.style.backgroundColor="#a9a9a9";
            }            
        }                
    }    
    document.body.onmousemove=function(){
        checkmargin();
    }
    document.body.onmouseup=function(){        
        if(selectarea.style.visibility=="visible"){
            endselect();
            selectChange();
        }
        destroyselect();        
    }  
}
